package com.lxg.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lxg.pojo.User;
import com.lxg.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陆小根
 * date: 2022/05/19
 * Description:
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

  @Autowired
  private CacheManager cacheManager;

  @Autowired
  private IUserService userService;

  /**
   * CachePut：将方法返回值放入缓存
   * value：缓存的名称，每个缓存名称下可以有多个key
   * key：缓存的key
   * @param user
   * @return
   */
  @CachePut(value = "userCache", key = "#user.id")
  @PostMapping
  public User save(User user) {
    userService.save(user);
    return user;
  }

  /**
   * CacheEvict：清理指定缓存
   * value：缓存的名称，每个缓存名称下面可以有多个key
   * key：缓存的key
   * @param id
   */
  @CacheEvict(value = "userCache", key = "#p0")
  // @CacheEvict(value = "userCache", key = "#root.args[0]")
  // @CacheEvict(value = "userCache", key = "#id")
  @DeleteMapping("/{id}")
  public void delete(@PathVariable Long id){
    userService.removeById(id);
  }


  // @CacheEvict(value = "userCache", key = "#p0.id")
  // @CacheEvict(value = "userCache", key = "#user.id")
  // @CacheEvict(value = "userCache", key = "#root.args[0].id")
  @CacheEvict(value = "userCache", key = "#result.id")
  @PutMapping
  public User update(User user){
    userService.updateById(user);
    return user;
  }

  /**
   * Cacheable:在方法执行前spring先看缓存中是否数据，如果有数据，则直接返回缓存数据，
   * 若没有数据，调用方法并返回值放到缓存中
   * value：缓存的名称，每个缓存名称下面可以有多个key
   * key：缓存的key
   * condition：条件，满足条件时才缓存数据
   * unless:满足条件则不缓存
   * @param id
   * @return
   */
  @Cacheable(value = "userCache", key = "#id", unless = "#result == null")
  @GetMapping("/{id}")
  public User getById(@PathVariable Long id){
    User user = userService.getById(id);
    return user;
  }

  @Cacheable(value = "userCache", key = "#user.id + '_' + #user.name")
  @GetMapping("/list")
  public List<User> list(User user){
    LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(user.getId() != null,User::getId,user.getId());
    queryWrapper.eq(user.getName() != null,User::getName,user.getName());
    List<User> list = userService.list(queryWrapper);
    return list;
  }

}
